/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.web.controller.multi;

import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.log.annotation.SysLog;
import com.medusa.gruul.platform.api.model.dto.WechatMiniLoginDTO;
import com.medusa.gruul.platform.model.dto.multi.MobileEncryptDataDTO;
import com.medusa.gruul.platform.service.IMultiClientWechatService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

/**
 * 多端登录控制器
 * @author 张治保
 * date 2021/11/27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/multi/auth/wechat")
public class MultiClientWechatController {

    private final IMultiClientWechatService multiClientWechatService;

    /**************** 开放平台 扫码登录 *****************/
    @SysLog("获取开放平台微信扫码登录二维码")
    @EscapeLogin
    @GetMapping("/open/login/qrcode/")
    public Result<String> openQrcodeUrl(@RequestParam @NotBlank(message = "跳转url不能为空") String url) {
        return Result.ok(
            multiClientWechatService.openQrcodeUrl(url)
        );
    }
    @SysLog("开放平台登录,获取loginId")
    @EscapeLogin
    @GetMapping("/open/login")
    public void openLogin(@RequestParam String code, @RequestParam String state, HttpServletResponse response) throws IOException {
        response.sendRedirect(
            multiClientWechatService.openLogin(code, state)
        );
    }

    /**************** 小程序code授权登录 *****************/
    @SysLog("微信小程序code登录,获取loginId")
    @EscapeLogin
    @GetMapping("/mini/login/{code}")
    public Result<WechatMiniLoginDTO> miniLogin(@PathVariable String code){
        return Result.ok(
            multiClientWechatService.miniLogin(code)
        );
    }

    @SysLog("解密加密信息手机号 并获取手机号")
    @EscapeLogin
    @PostMapping("/mini/mobile/login")
    public Result<String> decryptMobileAndLogin(@RequestBody MobileEncryptDataDTO encryptData){
        return Result.ok(
            multiClientWechatService.decryptMobile(encryptData)
        );
    }

}
